

package com.maoshi.shop.groupbuy.api.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.gson.JsonObject;
import com.maoshi.shop.bean.dto.OrderRefundDto;
import com.maoshi.shop.common.annotation.SysLog;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.response.ServerResponse;
import com.maoshi.shop.common.response.ServerResponseEntity;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.groupbuy.comment.api.dto.*;
import com.maoshi.shop.groupbuy.comment.enums.ActivityStatusEnum;
import com.maoshi.shop.groupbuy.comment.enums.TeamStatusEnum;
import com.maoshi.shop.groupbuy.comment.model.GroupTeam;
import com.maoshi.shop.groupbuy.comment.service.GroupActivityService;
import com.maoshi.shop.groupbuy.comment.service.GroupProdService;
import com.maoshi.shop.groupbuy.comment.service.GroupSkuService;
import com.maoshi.shop.groupbuy.comment.service.GroupTeamService;
import com.maoshi.shop.security.api.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.checkerframework.checker.units.qual.min;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 拼团商品接口
 *
 * @author 小懒虫
 * @date 2019/8/30 9:44
 */
@RestController
@RequestMapping("/groupProd")
@Api(tags = "拼团商品接口")
@AllArgsConstructor
public class GroupProdController {

    private final GroupSkuService groupSkuService;
    private final GroupTeamService groupTeamService;
    private final GroupProdService groupProdService;
    private final GroupActivityService groupActivityService;
    private final WxMaService wxMaService;

    @GetMapping("/info")
    @ApiOperation(value = "拼团活动详情", notes = "获取对应商品的拼团活动信息")
    public ResponseEntity<ServerResponse<ApiGroupActivityDto>> activityInfo(
            @ApiParam(value = "物品ID", required = true) @RequestParam("prodId") Long prodId,
            @ApiParam(value = "拼团活动ID", required = true) @RequestParam("groupActivityId") Long groupActivityId) {

        // 查询拼团活动信息
        ApiGroupActivityDto info = groupActivityService.getApiGroupActivityInfo(groupActivityId, prodId);

        // [getStatus 2:未启用]
        if (info == null || info.getStatus() == 2) {
//            ("当前商品没有参加该拼团活动");
            return ResponseEntity.ok(ServerResponseEntity.fail("当前商品没有参加该拼团活动"));
        }

        if (info.getIsPreheat() != 1 && info.getActivityStatus().equals(ActivityStatusEnum.NOT_STARTED.value())) {
            return ResponseEntity.ok(ServerResponseEntity.fail("当前拼团活动未开始"));
        }

        if (info.getActivityStatus().equals(ActivityStatusEnum.FINISHED.value())) {
            return ResponseEntity.ok(ServerResponseEntity.fail("当前拼团活动已结束"));
        }

        if (info.getActivityStatus().equals(ActivityStatusEnum.EXPIRED.value())) {
            return ResponseEntity.ok(ServerResponseEntity.fail("当前拼团活动已失效"));
        }

        ApiJoinGroupTeamDto joinTeam = groupTeamService.getJoinGroupTeamHasMaxNum(groupActivityId, prodId);
        if (joinTeam != null) {
            GroupTeam groupTeam = groupTeamService.getById(joinTeam.getJoinGroupTeamId());
            if (Objects.equals(groupTeam.getStatus(), TeamStatusEnum.IN_GROUP.value())) {
                info.setJoinGroupTeamId(joinTeam.getJoinGroupTeamId());
            }
            info.setProdCount(joinTeam.getProdCount());
        }

        // 获取SKU列表
        List<ApiGroupSkuDto> apiGroupSkuDtoList = groupSkuService.getApiByGroupActivityIdAndProdId(info.getGroupActivityId(), prodId);
        info.setGroupSkuList(apiGroupSkuDtoList);

        // 获取商品活动最低价
        List<Double> actPrices = apiGroupSkuDtoList.stream().map(ApiGroupSkuDto::getActPrice).collect(Collectors.toList());
        info.setActPrice(Collections.min(actPrices));

        return ResponseEntity.ok(ServerResponseEntity.success(info));
    }

    @GetMapping("/joinGroupList")
    @ApiOperation(value = "可加入的团列表", notes = "只显示最近n个团列表(默认10)")
    public ResponseEntity<List<ApiGroupTeamDto>> joinGroupList(
            @ApiParam(value = "物品ID", required = true) @RequestParam("prodId") Long prodId,
            @ApiParam(value = "拼团活动ID", required = true) @RequestParam("groupActivityId") Long groupActivityId,
            @ApiParam(value = "显示数量（默认10）") @RequestParam(value = "showSize", defaultValue = "10") Integer showSize) {
        List<ApiGroupTeamDto> list = groupTeamService.listJoinGroup(groupActivityId, prodId, showSize);
        return ResponseEntity.ok(list);
    }

    @GetMapping("/page")
    @ApiOperation(value = "拼团商品列表", notes = "拼团商品列表（分页列表）")
    public ResponseEntity<IPage<ApiGroupProdDto>> getGroupProdPage(PageParam<OrderRefundDto> page) {
        IPage<ApiGroupProdDto> prodPage = groupProdService.getApiGroupProdPage(page);
        return ResponseEntity.ok(prodPage);
    }

    @GetMapping("/indexList")
    @ApiOperation(value = "首页中的团购商品列表", notes = "获取最新前5条团购商品数据")
    public ResponseEntity<List<ApiGroupProdDto>> indexList() {
        PageParam<OrderRefundDto> page = new PageParam<>();
        page.setSearchCount(false);
        page.setCurrent(1L);
        page.setSize(5);
        page.setTotal(5);
        IPage<ApiGroupProdDto> prodPage = groupProdService.getApiGroupProdPage(page);
        return ResponseEntity.ok(prodPage.getRecords());
    }
}
